#include "../Tools/GLHeaders.hpp" 
#include "DisplayFunc.hpp"

#include <cmath>
#include <cstdlib>

#include "../Global.hpp"
#include "../Tools/Chrono.hpp"
#include "../Tools/Background.hpp"
#include "../Tools/Foreground.hpp"
#include "../Tools/ReferenceCube.hpp"
#include "../Tools/ReferenceFrame.hpp"

#include "../Terrain/Terrain.hpp"

void DrawGLScene(void){
  static Chrono time;
  static bool init = false;
  static Terrain terrain;

  if(!init){    
    time.Reset();
    time.Activate();
    init = true;
  }
 
  glClearColor(0.2, 0.2, 0.2, 1.0);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glLoadIdentity();
  glTranslatef(0.0f,0.0f,-3.0f);
  glRotatef(100*time.GetTime(), cosf(time.GetTime()), cos(-time.GetTime()), sinf(time.GetTime()));
  //glRotatef(50.0f*time.GetTime(), 0.0f, 0.0f, 1.0f);

  Background.Draw();
  ReferenceFrame.Draw();
  
  glEnable(GL_TEXTURE_2D);
  //glBindTexture(GL_TEXTURE_2D, *(gl_tex+(((int)roundf(time.GetTime()/3.0f))%6)));
  glBindTexture(GL_TEXTURE_2D, *(gl_tex+4));
  glBegin(GL_TRIANGLES);
  glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
  glNormal3f(0.0f, 0.0f, 1.0f);
  glTexCoord2f(-1.0f,-1.0f); glVertex3f(-1.0f,-1.0f, 0.0f);
  glTexCoord2f( 1.0f,-1.0f); glVertex3f( 1.0f,-1.0f, 0.0f);
  glTexCoord2f( 1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 0.0f);

  glTexCoord2f(-1.0f,-1.0f); glVertex3f(-1.0f,-1.0f, 0.0f);
  glTexCoord2f( 1.0f, 1.0f); glVertex3f(1.0f, 1.0f, 0.0f);
  glTexCoord2f(-1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 0.0f);
  glEnd();

  terrain.Draw(fabsf(cosf(time.GetTime()*3/25.0f)), fabs(cosf(time.GetTime()*5/25.0f)), fabsf(cosf(time.GetTime()*7/25.0f)), 1.0f);
  //ReferenceCube.Draw(); 

  glutSwapBuffers(); 
}
